<?php

namespace app\common\library\aliyunsms;


class SendSms
{
    private $sms;
    private $error;
    private $templateCode = [
        "1" => [
            'code'  => "SMS_177248058",
            'desc'  => '用户注册',
            'param' => ['code'],
        ],
        "2" => [
            'code'  => "SMS_177257982",
            'desc'  => '短信通知',
            'param' => ['code'],
        ],
        "3" => [
            'code'  => "SMS_177248058",
            'desc'  => '短信通知',
            'param' => ['code'],
        ],
        "4" => [
            'code'  => "SMS_177253025",
            'desc'  => '短信通知',
            'param' => ['code'],
        ],
    ];

    public function __construct()
    {
        $config    = config('sms.');
        $this->sms = new Sms($config);
    }

    /**
     * 手机短信发送验证
     * @return [type] [description]
     */
    public function sendCode($type, $mobile, $code)
    {
        $param['code'] = $code;

        return $this->send($type, $mobile, $param);
    }

    public function getError()
    {
        return $this->error;
    }

    /**
     * 短信通知
     * @return [type] [description]
     */
    public function send($type, $mobile, $param)
    {
        $template      = isset($this->templateCode[$type]) ? $this->templateCode[$type] : $this->templateCode['0'];
        $TemplateParam = [];
        if(!empty($template['param'])){
            foreach($template['param'] as $key){
                $TemplateParam[$key] = $param[$key];
            }
        }
        $result      = $this->sms->send($template['code'], $mobile, $TemplateParam);
        $this->error = $this->sms->getError();

        return $result;
    }
}